package cn.yuanqiao.manage.model;

import cn.yuanqiao.common.constant.SysContants;

import cn.yuanqiao.common.core.domain.entity.SysRole;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 15861
 */
@Component
public class ManageSysModel {


    private static Long[] sysadmin;


    private static Long[] syssso;


    private static Long[] sysauditor;

    private static ISysMenuService remoteMenuService;

    @Resource
    private void setRemoteMenuService(ISysMenuService remoteMenuService) {
        ManageSysModel.remoteMenuService = remoteMenuService;
    }

    @Value("${templateMenuIds.sysadmin}")
    private void setSysadmin(Long[] sysadmin) {
        ManageSysModel.sysadmin = sysadmin;
    }

    @Value("${templateMenuIds.syssso}")
    private void setSyssso(Long[] syssso) {
        ManageSysModel.syssso = syssso;
    }

    @Value("${templateMenuIds.sysauditor}")
    private void setSysauditor(Long[] sysauditor) {
        ManageSysModel.sysauditor = sysauditor;
    }

    public static List<SysRole> getSysRoleList(String tenantCode){
        ArrayList<SysRole> list = new ArrayList<>();
        list.add(getSysRole("系统管理员",tenantCode+"_"+ SysContants.SYSADMINFLAG,tenantCode));
        list.add(getSysRole("安全保密员",tenantCode+"_"+SysContants.SYSSSOFLAG,tenantCode));
        list.add(getSysRole("安全审计员",tenantCode+"_"+SysContants.SYSAUDITORFLAG,tenantCode));
        return list;
    }

    public static List<SysUser> getSysUserList(String tenantCode){
        ArrayList<SysUser> list = new ArrayList<>();
        list.add(getSysUser("sysadmin","系统管理员",tenantCode));
        list.add(getSysUser("syssso","安全保密员",tenantCode));
        list.add(getSysUser("sysauditor","安全审计员",tenantCode));
        return list;
    }

    public static SysUser getSysUser(String userName,String nickName,String tenantCode){
        SysUser sysUser = new SysUser();
        sysUser.setUserName(userName);
        sysUser.setNickName(nickName);
        sysUser.setPassword("123456");
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setTenantCode(tenantCode);
        return sysUser;
    }

    public static SysRole getSysRole(String roleName,String roleKey,String tenantCode){
        SysRole sysRole = new SysRole();
        sysRole.setDataScope("1");
        sysRole.setRoleName(roleName);
        sysRole.setRoleKey(roleKey);
        sysRole.setRoleSort(0);
        sysRole.setDataScope("1");
        sysRole.setTenantCode(tenantCode);
        sysRole.setMenuIds(getMenuIds(roleName,tenantCode));
        sysRole.setStatus("0");
        sysRole.setDelFlag("0");
        return sysRole;
    }

    public static Long[] getMenuIds(String roleName,String tenantCode){
        //TODO 给三员授予菜单权限
        if ("系统管理员".equals(roleName)){
            return remoteMenuService.getTemplateMenuIds("1",tenantCode);
        }else if ("安全保密员".equals(roleName)){
            return remoteMenuService.getTemplateMenuIds("2",tenantCode);
        }else if ("安全审计员".equals(roleName)){
            return remoteMenuService.getTemplateMenuIds("3",tenantCode);
        }else {
            return null;
        }
    }

}
